001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Feb 14, 2003
005     * Time: 9:34:32 PM
006     */
007    
008    package EVolve.util.painters.shapes;
009    
010    import java.awt.*;
011    import java.util.*;
012    
013    public abstract class Shape implements Cloneable{
014        public int x, y;
015        protected long entity_id;
016        protected long entity_type;
017        protected HashMap consumerMap;
018        protected Color color;
019    
020        public Shape(long entity_type, long entity_id) {
021            this.entity_id = entity_id;
022            this.entity_type = entity_type;
023            consumerMap = new HashMap();
024            color = null;
025        }
026    
027        public long getEntityID() {
028            return entity_id;
029        }
030    
031        public Rectangle getPosition() {
032            return new Rectangle(x,y,1,1);
033        }
034    
035        public void move(int x, int y) {
036            this.x = x;
037            this.y = y;
038        }
039    
040        public void addConsumer(Shape consumer, int gravity) {
041            consumerMap.put(consumer,new Integer(gravity));
042        }
043    
044        public HashMap getConsumers() {
045            return consumerMap;
046        }
047    
048        public long getEntityType() {
049            return entity_type;
050        }
051    
052        public void setColor(Color color) {
053            this.color = color;
054        }
055    
056        public Color getColor() {
057            return color;
058        }
059    
060        protected void drawArrows(Graphics2D g2) {
061            Iterator it2 = consumerMap.keySet().iterator();
062            while (it2.hasNext()) {
063                Shape consumer = (Shape)it2.next();
064                int gravity = ((Integer)consumerMap.get(consumer)).intValue();
065                // draw arrows
066                int x1 = x+getWidth()/2, y1 = y+getHeight()/2;
067                int x2 = consumer.x + consumer.getWidth()/2, y2 = consumer.y + consumer.getHeight()/2;
068                g2.drawLine(x1,y1,x2,y2);
069                double len = java.lang.Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
070                double angle = java.lang.Math.asin(java.lang.Math.abs(y1-y2)/len);
071                if (x1>x2) {
072                    if (y1 < y2)
073                        angle = 2*java.lang.Math.PI - angle;
074                } else {
075                    if (y1 > y2)
076                        angle = java.lang.Math.PI - angle;
077                    else
078                        angle = java.lang.Math.PI + angle;
079                }
080                g2.drawLine(x2,y2,(int)(x2+10*java.lang.Math.cos(angle-0.5236)),(int)(y2+10*java.lang.Math.sin(angle-0.5236)));
081                g2.drawLine(x2,y2,(int)(x2+10*java.lang.Math.cos(angle+0.5236)),(int)(y2+10*java.lang.Math.sin(angle+0.5236)));
082                g2.drawString(""+gravity+"",(x1+(x2-x1)/5),(y1-(y1-y2)/5));
083            }
084        }
085    
086        public Object clone() {
087            Shape o = null;
088            try {
089                o = (Shape)super.clone();
090            } catch (CloneNotSupportedException e) {
091                e.printStackTrace();
092                return null;
093            }
094            o.consumerMap = new HashMap();
095            return o;
096        }
097    
098        public abstract void draw(Graphics2D g);
099    
100        public abstract void fill(Graphics2D g);
101    
102        public abstract String getName();
103    
104        public abstract boolean insideShape(int x, int y);
105    
106        public abstract int getWidth();
107    
108        public abstract int getHeight();
109    
110        public abstract void setSize(int width, int height);
111    }